import 'package:flutter/material.dart';

class TabControllerPage extends StatefulWidget {
  @override
  _TabControllerPageState createState() => _TabControllerPageState();
}

// 多继承
class _TabControllerPageState extends State<TabControllerPage> with SingleTickerProviderStateMixin {

  TabController _tabController;
  @override
  void initState() { // 声明周期函数，组件加载的时候就会触发
    super.initState();
    this._tabController = new TabController(
        length: 2,
        vsync: this
    );
    // 监听事件
    _tabController.addListener(() {
      print(_tabController.index);
    });
  }

  @override
  void dispose() { // 声明周期函数，组件销毁的时候就会触发
    // TODO: implement dispose
    super.dispose();
    this._tabController.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('TabControllerPage'),
        bottom: TabBar(
          controller: this._tabController, // 注意
          tabs: <Widget>[
            Text('热销'),
            Text('推荐'),
          ],
        ),
      ),
      body: TabBarView(
        controller: this._tabController, // 注意
        children: <Widget>[
          Center(
            child: Text('热销aaaaaaa'),
          ),
          Center(
            child: Text('推荐bbbbbbb'),
          )
        ],
      ),
    );
  }
}
